package com.cxt.utils;

import com.cxt.model.Coordinate;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Map;

public class CoordinateUtil {
    //根据字符串生成坐标
    public static Coordinate StringToCoordinate(String coordinate) {
        Coordinate topLeftCoordinate = Coordinate.builder()
                .lng(new BigDecimal(coordinate.split(",")[0]))
                .lat(new BigDecimal(coordinate.split(",")[1]))
                .build();
        return topLeftCoordinate;
    }
    //根据Coordinate生成字符串坐标
    public static String CoordinateToString(Coordinate coordinate){
        return coordinate.getLng()+","+coordinate.getLat();
    }
    //根据字符串生成矩形坐标Map
    public static Map<String,Coordinate> StringToCoordinateMap(String topLeft, String bottomRight) {
        if(topLeft == null || bottomRight == null || topLeft.equals("") || bottomRight.equals("")) {
            return null;
        }
        Coordinate topLeftCoordinate = Coordinate.builder()
                .lng(new BigDecimal(topLeft.split(",")[0]))
                .lat(new BigDecimal(topLeft.split(",")[1]))
                .build();
        Coordinate bottomRightCoordinate = Coordinate.builder()
                .lng(new BigDecimal(bottomRight.split(",")[0]))
                .lat(new BigDecimal(bottomRight.split(",")[1]))
                .build();
        return Map.of("topLeft", topLeftCoordinate, "bottomRight", bottomRightCoordinate);

    }

    /**
     *  根据输入的地点坐标计算中心点
     * @param geoCoordinateList
     * @return
     */
    public static Coordinate getCenterPoint(List<Coordinate> geoCoordinateList) {
        int total = geoCoordinateList.size();
        BigDecimal X = BigDecimal.valueOf(0.0), Y =  BigDecimal.valueOf(0.0), Z =  BigDecimal.valueOf(0.0);
        for (Coordinate g : geoCoordinateList) {
            BigDecimal lat, lon, x, y, z;

            lat = g.getLat().multiply(BigDecimal.valueOf( Math.PI / 180));
            lon = g.getLng().multiply(BigDecimal.valueOf( Math.PI / 180));
            x = BigDecimal.valueOf(
                    Math.cos(Double.parseDouble(lat.toString())) * Math.cos(Double.parseDouble(lon.toString()))
            );
            y = BigDecimal.valueOf(
                    Math.cos(Double.parseDouble(lat.toString())) * Math.sin(Double.parseDouble(lon.toString()))
            );
            z = BigDecimal.valueOf( Math.sin(Double.parseDouble(lat.toString())));
            X = X.add(x);
            Y = Y.add(y);
            Z = Z.add(z);
        }

        X = X.divide(BigDecimal.valueOf(total), 6, RoundingMode.HALF_UP);
        Y = Y.divide(BigDecimal.valueOf(total), 6, RoundingMode.HALF_UP);
        Z = Z.divide(BigDecimal.valueOf(total), 6, RoundingMode.HALF_UP);
//        double Lon = Math.atan2(Y, X);
        BigDecimal Lon= BigDecimal.valueOf(Math.atan2(Double.parseDouble(Y.toString()), Double.parseDouble(X.toString())));
//        double Hyp = Math.sqrt(X * X + Y * Y);
        BigDecimal Hyp = BigDecimal.valueOf(Math.sqrt(
                Double.parseDouble(X.toString()) * Double.parseDouble(X.toString()) + Double.parseDouble(Y.toString()) * Double.valueOf(Y.toString())
        ));
//        double Lat = Math.atan2(Z, Hyp);
        BigDecimal Lat = BigDecimal.valueOf(Math.atan2(Double.parseDouble(Z.toString()), Double.parseDouble(Hyp.toString())));
        Coordinate center = Coordinate.builder()
                .lng(Lon.multiply(BigDecimal.valueOf(180L)).divide(BigDecimal.valueOf(Math.PI), 6, RoundingMode.HALF_UP))
                .lat(Lat.multiply(BigDecimal.valueOf(180L)).divide(BigDecimal.valueOf(Math.PI), 6, RoundingMode.HALF_UP))
                .build();
        return center;
    }
}
